﻿package mortal.game.scene3D.util
{
    import com.gengine.global.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.entity.*;

    public class MoveUtil extends Object
    {
        public static var lastMoveTime:int = 0;
        public static var speedIntervalBase:Number = 16.6667;
        public static var speedInterval:Number = 16.6667;
        public static var dif:Number = 0;
        private static var _isActive:Boolean = true;

        public function MoveUtil()
        {
            return;
        }// end function

        public static function init() : void
        {
            Global.stage.addEventListener(ThrottleEvent.THROTTLE, onThrottleHandler, false, 99998);
            return;
        }// end function

        public static function setLastMoveTime() : void
        {
            lastMoveTime = getTimer() - speedIntervalBase;
            return;
        }// end function

        private static function onThrottleHandler(event:ThrottleEvent) : void
        {
            switch(event.type)
            {
                case ThrottleType.THROTTLE:
                {
                    _isActive = event.targetFrameRate >= 20;
                    if (_isActive)
                    {
                        lastMoveTime = getTimer();
                    }
                    dif = 0;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public static function updateEntityPos() : void
        {
            var _loc_2:IEntity = null;
            var _loc_3:int = 0;
            var _loc_4:Number = NaN;
            if (lastMoveTime == 0)
            {
                lastMoveTime = getTimer();
                return;
            }
            if (_isActive)
            {
                speedIntervalBase = 1000 / Global.stage.frameRate;
            }
            else
            {
                speedIntervalBase = 1000 / 60;
            }
            var _loc_1:* = getTimer();
            speedInterval = speedIntervalBase;
            if (_isActive)
            {
                _loc_3 = _loc_1 - lastMoveTime - speedIntervalBase;
                if (_loc_3 > 180)
                {
                    _loc_3 = 180;
                }
                dif = dif + _loc_3;
                _loc_4 = 0;
                if (dif > 2 * speedIntervalBase)
                {
                    _loc_4 = dif * 0.05;
                }
                else if (dif > speedIntervalBase)
                {
                    _loc_4 = 0.1 * speedIntervalBase;
                }
                speedInterval = speedIntervalBase + _loc_4;
                dif = dif - _loc_4;
            }
            lastMoveTime = _loc_1;
            for each (_loc_2 in ThingUtil.entityUtil.entitysMap.allEntitys)
            {
                
                if (_loc_2 is MovePlayer)
                {
                    (_loc_2 as MovePlayer).updatePos();
                }
            }
            return;
        }// end function

    }
}
